<% content_for :page_title do %>
  <%= page_header_back_button spree.edit_admin_order_path(@reimbursement.order) %>
  <div class="d-flex gap-3 align-items-center">
    <span><%= Spree.t(:reimbursement) %></span>
    <span><%= @reimbursement.number %></span>

    <span class="badge badge-<%= reimbursement_status_color(@reimbursement) %>">
      <%= @reimbursement.reimbursement_status %>
    </span>
  </div>
<% end %>

<%= render partial: "spree/admin/shared/error_messages",
locals: {
  target: @reimbursement,
} %>

<%= form_for [:admin, @order, @reimbursement] do |f| %>
  <div class="card mb-4">
    <div class="card-header">
      <h5><%= Spree.t(:items_to_be_reimbursed) %></h5>
    </div>
    <div class="card-body p-0">
      <table class="table table-condensed reimbursement-return-items-table">
        <thead>
          <tr>
            <th scope="col"><%= Spree.t(:product) %></th>
            <th scope="col"><%= Spree.t(:preferred_reimbursement_type) %></th>
            <th scope="col"><%= Spree.t(:reimbursement_type_override) %></th>
            <th scope="col"><%= Spree.t(:pre_tax_refund_amount) %></th>
            <th scope="col"><%= Spree.t(:total) %></th>
            <th scope="col"><%= Spree.t(:exchange_for) %></th>
          </tr>
        </thead>
        <tbody>
          <%= f.fields_for :return_items, @reimbursement.return_items.sort_by(&:id) do |item_fields| %>
            <% return_item = item_fields.object %>
            <tr>
              <td>
                <%= render "spree/admin/variants/variant",
                variant: return_item.inventory_unit.variant %>
              </td>
              <td>
                <%= reimbursement_type_name(return_item.preferred_reimbursement_type) %>
              </td>
              <td>
                <%= item_fields.select(
                  :override_reimbursement_type_id,
                  reimbursement_types.collect { |r| [r.name.humanize, r.id] },
                  { include_blank: true },
                  { data: { controller: "autocomplete-select" } },
                ) %>
              </td>
              <td>
                <%= item_fields.text_field :pre_tax_amount,
                                       { class: "refund-amount-input form-control" } %>
              </td>
              <td>
                <%= return_item.display_total %>
              </td>
              <td>
                <% if return_item.exchange_processed? %>
                  <%= return_item.exchange_variant.exchange_name %>
                <% else %>
                  <%= item_fields.collection_select :exchange_variant_id,
                                                return_item.eligible_exchange_variants,
                                                :id,
                                                :exchange_name,
                                                { include_blank: true },
                                                {
                                                  class:
                                                    "custom-select return-item-exchange-selection",
                                                } %>
                <% end %>
              </td>
            </tr>
          <% end %>
        </tbody>
      </table>
    </div>
  </div>

  <div class="form-actions filter-actions actions mb-4">
    <%= turbo_save_button_tag Spree.t("actions.update"), class: "btn btn-primary" %>
  </div>
<% end %>

<div class="card mb-4">
  <div class="card-header">
    <h5><%= Spree.t(:calculated_reimbursements) %></h5>
  </div>
  <div class="card-body p-0">
    <table class="table table-condensed">
      <thead>
        <tr>
          <th scope="col"><%= Spree.t(:reimbursement_type) %></th>
          <th scope="col"><%= Spree.t(:description) %></th>
          <th scope="col"><%= Spree.t(:amount) %></th>
        </tr>
      </thead>
      <tbody>
        <% @reimbursement_objects.each do |reimbursement_object| %>
          <tr data-hook="reimbursement_reimbursement_object_row">
            <td><%= reimbursement_object.class.name.demodulize %></td>
            <td><%= reimbursement_object.description %></td>
            <td><%= reimbursement_object.display_amount %></td>
          </tr>
        <% end %>
      </tbody>
    </table>
  </div>
</div>

<% if @order.has_non_reimbursement_related_refunds? %>
  <div class="alert alert-danger mb-2">
    <%= "#{Spree.t("note")}: #{Spree.t("this_order_has_already_received_a_refund")}. #{Spree.t("make_sure_the_above_reimbursement_amount_is_correct")}." %>
  </div>
<% end %>

<div class="form-actions">
  <% if !@reimbursement.reimbursed? %>
    <%= button_to [:perform, :admin, @order, @reimbursement], { class: 'btn btn-primary', method: 'post' } do %>
      <%= icon "money" %>
      <%= Spree.t(:reimburse) %>
    <% end %>
    <span class="or"><%= Spree.t(:or) %></span>
    <%= link_to Spree.t("actions.cancel"),
    url_for([:edit, :admin, @order, @reimbursement.customer_return]) %>
  <% end %>
</div>
